<?php
require_once "class_Main.php";

	class Index extends Main{
	
		protected $connect;
		private $ord_opt = array ('Order A-Z'=>'title ASC', 'Order Z-A'=>'title DESC', 'Order 0-9'=>'issue_date ASC', 'Order 9-0'=>'issue_date DESC');
		
		public function __construct(){
			$this->connect = parent::connect();
		}
	
		public function get ($totaloffset, $perpage, $cat, $ord='title ASC'){
			if($cat==='bin'){
				$get = "SELECT id, title, short_descr, issue_date, img FROM store  WHERE bin=1 ORDER BY $ord";
			}
			else{
				$get = "SELECT id, title, price, img FROM store WHERE category=$cat ORDER BY $ord LIMIT $totaloffset, $perpage";
			}
			$result = mysql_query($get, $this->connect) or die ('Cant result');
			$i=0;
			while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)){
				$data[$i] = $row;
				$i++;
			}
			mysql_free_result($result);
			if (isset($data)) {return $data;}
		}
		
		public function get_all($id){
			if (isset($id)){
				$get = "SELECT * FROM store WHERE id=$id";
				$result = mysql_query($get, $this->connect);
				$all = mysql_fetch_array($result,  MYSQL_ASSOC);
				return $all;
				mysql_free_result($result);
			}
		}
		
		public function get_total_cat($cat){
			$get = "SELECT COUNT(category) AS total FROM store WHERE category=$cat";
			$result=mysql_query($get, $this->connect);
			$total=mysql_fetch_array($result, MYSQL_NUM);
			return $total=$total[0];
		}
	
		public function filter($ord){
		$options="";
			foreach ($this->ord_opt as $key=>$value){
			if (preg_match("/^$value$/", $ord)){	
					$options .= "<option value=\"$value\" selected=\"selected\">$key</option>";
				}
				else{
					$options .= "<option value=\"$value\">$key</option>";
				}
			}
			return $options;
		}
		
		/*
		public function __destruct (){
		echo "Im destruct";
		exit();
			parent::__destruct();
		}
		*/
	}
?>